package com.itheima;

import com.itheima.domain.Active;
import com.itheima.domain.Score;
import com.itheima.domain.User;
import com.itheima.service.ActiveService;
import com.itheima.service.ScoreService;
import com.itheima.service.UserService;
import com.itheima.spring.MySpring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.scheduling.annotation.Async;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;


public class app {
    static ApplicationContext applicationContext=new AnnotationConfigApplicationContext(MySpring.class);
    static UserService userService = applicationContext.getBean(UserService.class);
    static ScoreService scoreService = applicationContext.getBean(ScoreService.class);
    static ActiveService activeService = applicationContext.getBean(ActiveService.class);
    public static void main(String[] args) throws IOException {

        String username="lisi12345";
        String pwd="123456";
        if (username!=null && pwd !=null){
            //往用户表中增加数据
            saveUser(username, pwd);
            //添加完把该用户id查询出来
            User user =findByUsername(username);
            final Integer id = user.getId();

            //往积分表中添加数据
            new Thread(new Runnable() {
                public void run() {
                        addScore(id);
                }
            },"t1").start();

            //往行为表中添加数据
            new Thread(new Runnable() {
                public void run() {
                    addActive(id);
                }
            },"t2").start();

        }else {
            //账号或密码未输入
            System.out.println("账号或密码有误，请重新注册");
        }
    }

    private static void addActive(int id) {
        Active active = new Active();
        active.setUid(id);
        active.setTime(new Date());
        String ac ="啥也没干";
        active.setActive(ac);
        activeService.saveUser(active);
    }


    public static void addScore(int id) {
        Score score = new Score();
        score.setUid(id);
        score.setNum(0);
        scoreService.saveScore(score);
    }

    private static User findByUsername(String username) {
        User user = userService.findByUsername(username);
        return user;
    }

    private static void saveUser(String user, String pwd) {
        User u = new User();
        u.setUser(user);
        u.setPwd(pwd);
        u.setDate_time(new Date());
        userService.saveUser(u);
    }

}
